package srm510;

public class TheLuckyGameDivTwo {
    public int find(int a, int b, int jLen, int bLen) {
        int tot = 0;
        int val[] = new int[b - a + 1];
        boolean lucky;
        for (int i = a; i <= b; i++) {
            lucky = true;
            int num = i;
            while (num > 0) {
                if (num % 10 != 7 && num % 10 != 4) {
                    lucky = false;
                    break;
                }
                num = num / 10;
            }
            if (lucky)
                tot++;
            val[i - a] = tot;
        }
        
        int max = 0;
        for (int i = a; i + jLen - 1 <= b; i++) {
            int min = Integer.MAX_VALUE, v, endInterval, beginInterval;
            for (int j = i; j + bLen - 1 <= i + jLen - 1; j++) {
                endInterval = j + bLen - 1;
                beginInterval = j;
                v = val[endInterval - a];
                if (j > a) v -= val[beginInterval - a - 1];
                if (v < min) min = v;
            }
            if (min > max) max = min;
        }
        return max;
    }

    // BEGIN KAWIGIEDIT TESTING
    // Generated by KawigiEdit 2.1.8 (beta) modified by pivanof
    private static boolean KawigiEdit_RunTest(int testNum, int p0, int p1, int p2, int p3, boolean hasAnswer, int p4) {
        System.out.print("Test " + testNum + ": [" + p0 + "," + p1 + "," + p2 + "," + p3);
        System.out.println("]");
        TheLuckyGameDivTwo obj;
        int answer;
        obj = new TheLuckyGameDivTwo();
        long startTime = System.currentTimeMillis();
        answer = obj.find(p0, p1, p2, p3);
        long endTime = System.currentTimeMillis();
        boolean res;
        res = true;
        System.out.println("Time: " + (endTime - startTime) / 1000.0 + " seconds");
        if (hasAnswer) {
            System.out.println("Desired answer:");
            System.out.println("\t" + p4);
        }
        System.out.println("Your answer:");
        System.out.println("\t" + answer);
        if (hasAnswer) {
            res = answer == p4;
        }
        if (!res) {
            System.out.println("DOESN'T MATCH!!!!");
        } else if ((endTime - startTime) / 1000.0 >= 2) {
            System.out.println("FAIL the timeout");
            res = false;
        } else if (hasAnswer) {
            System.out.println("Match :-)");
        } else {
            System.out.println("OK, but is it right?");
        }
        System.out.println("");
        return res;
    }

    public static void main(String[] args) {
        boolean all_right;
        all_right = true;

        int p0;
        int p1;
        int p2;
        int p3;
        int p4;

        // ----- test 0 -----
        p0 = 1;
        p1 = 10;
        p2 = 2;
        p3 = 1;
        p4 = 0;
        all_right = KawigiEdit_RunTest(0, p0, p1, p2, p3, true, p4) && all_right;
        // ------------------

        // ----- test 1 -----
        p0 = 1;
        p1 = 100;
        p2 = 100;
        p3 = 100;
        p4 = 6;
        all_right = KawigiEdit_RunTest(1, p0, p1, p2, p3, true, p4) && all_right;
        // ------------------

        // ----- test 2 -----
        p0 = 4;
        p1 = 8;
        p2 = 3;
        p3 = 2;
        p4 = 1;
        all_right = KawigiEdit_RunTest(2, p0, p1, p2, p3, true, p4) && all_right;
        // ------------------

        // ----- test 3 -----
        p0 = 1;
        p1 = 100;
        p2 = 75;
        p3 = 50;
        p4 = 2;
        all_right = KawigiEdit_RunTest(3, p0, p1, p2, p3, true, p4) && all_right;
        // ------------------

        if (all_right) {
            System.out.println("You're a stud (at least on the example cases)!");
        } else {
            System.out.println("Some of the test cases had errors.");
        }
    }
    // END KAWIGIEDIT TESTING
}
//Powered by KawigiEdit 2.1.8 (beta) modified by pivanof!
